﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness.InterFace;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMS.AdminTemplate
{
    [Function("Admin.ReportStatus")]
    public partial class ReportStatus :PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadDefault();
            }

        }

        private void LoadDefault()
        {
            ControlHelper.BindPublicItemsByTypeID(listReportStatus, DictionaryCategoryConst.ReportStatusID,EmptyType.None);
            ControlHelper.BindPublicItemsByTypeID(chkButtonEvent, DictionaryCategoryConst.ButtonPermissionID, EmptyType.None);
        }

        protected void listReportStatus_OnSelectedIndexChanged(object sender,EventArgs e)
        {

            foreach (ListItem item in chkButtonEvent.Items)
            {
                item.Selected = false;
            }
            if (listReportStatus.SelectedValue.Trim().Length > 0)
            {
                var collection = new ReportStatusToButtonEventCollection();
                collection.GetMulti(ReportStatusToButtonEventFields.ReportStatusId ==
                                       int.Parse(listReportStatus.SelectedValue));

                foreach (var reportToEnvent in collection)
                {
                    foreach (ListItem item in chkButtonEvent.Items)
                    {
                        if (item.Value==reportToEnvent.ButtonEventId.ToString())
                        {
                            item.Selected = true;
                        }
                    }
                }
            }
        }

       protected void btnSaveEventToReportStatus_Click(object sender,EventArgs e)
       {
           if(listReportStatus.SelectedValue.Trim().Length>0)
           {

               var collection = new ReportStatusToButtonEventCollection();
               collection.DeleteMulti(ReportStatusToButtonEventFields.ReportStatusId ==
                                      int.Parse(listReportStatus.SelectedValue));

               foreach (ListItem item in chkButtonEvent.Items)
               {
                   if(item.Selected)
                   {
                       var reportToEvent = new ReportStatusToButtonEventEntity
                                               {
                                                   ReportStatusId = int.Parse(listReportStatus.SelectedValue),
                                                   ButtonEventId = int.Parse(item.Value)
                                               };
                       reportToEvent.Save();

                   }
               }

               Alert(GenerateAlert.SavSuccess);
           
           }
           else
           {
               Alert("Please select a report status.");
           }
       }
    }
}
